const Pool = require('./../mysql/mysql');
const connect = Pool.Pool;

/*
* 视频列表的增删改查
* */

function videos() {
    //获取所有视频
    this.getAll = (callback) => {
        let sql = 'select * from videos';
        connect.getConnection((err,connection) => {
            if (err){
                console.log('连接失败');
                return;
            } else{
                connection.query(sql,(error,rows,field) => {
                    if (error){
                        console.log(error);
                        return;
                    }else{
                        connection.release(); //每一次用完都要释放
                        callback(rows);
                    }
                })
            }
        })
    }
    //删除指定视频
    this.delVideo = (id,callback) => {
        let sql = 'delete from videos where id= '+id;
        connect.getConnection((err, connection) => {
            if (err) {
                console.log('连接失败');
                return;
            } else {
                connection.query(sql, function (error, row, field) {
                    if (error) {
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
    //批量删除视频
    this.delAllVideo = (params, callback) => {
        let arr = params.data.split(',');
        arr.forEach(item => {
            let sql = 'delete from videos where id='+item;
            connect.getConnection((err, connection) => {
                if (err) {
                    console.log('连接失败');
                    return;
                } else {
                    connection.query(sql, function (error, row, field) {
                        if (error) {
                            console.log('查询失败' + error.message);
                            return;
                        }
                        connection.release(); //每一次用完都要释放
                        callback(row);
                    });
                }
            })
        })
    }
    //查询视频
    this.search = (params, callback) => {
        let sql = 'select * from videos where type='+params.type;
        connect.getConnection((err, connection) => {
            if (err) {
                console.log('连接失败');
                return;
            } else {
                connection.query(sql, function (error, row, field) {
                    if (error) {
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
    //新增视频列表
    this.add = (params,callback) => {
        let sql = 'insert into videos set ?',data = {
            title: params.title,
            createDate: String(new Date().getTime()),
            source: params.source,
            type: params.type,
            cover:　params.cover,
            desc: params.desc
        };
        connect.getConnection((err, connection) => {
            if (err) {
                console.log('连接失败');
                return;
            } else {
                connection.query(sql,data, function (error, row, field) {
                    if (error) {
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        })
    }
}
module.exports = videos;
